//
//  LookingForDoctorViewController.m
//  Doctor1.0
//
//  Created by fun on 15/11/7.
//  Copyright © 2015年 cocoa-mk. All rights reserved.
//

#import "LookingForDoctorViewController.h"
#import "LookingDetailsViewController.h"
#import "LookingForDoctorCell.h"

@interface LookingForDoctorViewController ()
{
    NSInteger selectFatherIndex;
    NSInteger selectSubIndex;//子title
    NSInteger selectThreeIndex;//三级子title
}

@end

@implementation LookingForDoctorViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    viewNavBg.backgroundColor = [UIColor whiteColor];
    titleLabel.textColor = defaultNavBackColor;
    titleLabel.text = @"按疾病找名医";
    SetBtnImages(btnBack, @"left_back_green_icon.png", @"");
    
    self.view.backgroundColor = defaultNavBackColor;
    
    selectSubIndex = 0;
    [self setTypeButton];
}


#pragma mark 设置类别btn
-(void)setTypeButton
{
    for (UIButton * viewBtn in [self.view subviews])
    {
        if ([viewBtn isKindOfClass:[UIButton class]])
        {
            if (viewBtn.tag == self.selectFatherID)
            {
                viewBtn.backgroundColor = defaultNavBackColor;
                [viewBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            }else
            {
                viewBtn.backgroundColor = [UIColor whiteColor];
                [viewBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
            }
        }
    }
    
    for(int i=0; i<self.arrayTitle.count; i++)
    {
        NSMutableDictionary * dic = [self.arrayTitle objectAtIndex:i];
        if ([[dic objectForKey:@"id"] intValue] == self.selectFatherID)
        {
            selectFatherIndex = i;
            break;
        }
    }
    
}


-(IBAction)btnTypeClick:(id)sender
{
    UIButton * btn = (UIButton *)sender;
    if (self.selectFatherID != btn.tag)
    {
        self.selectFatherID = btn.tag;
        selectSubIndex = 0;
        for(int i=0; i<self.arrayTitle.count; i++)
        {
            NSMutableDictionary * dic = [self.arrayTitle objectAtIndex:i];
            if ([[dic objectForKey:@"id"] intValue] == self.selectFatherID)
            {
                selectFatherIndex = i;
                break;
            }
        }
        
        [tableTitle reloadData];//刷新字表
        [self setTypeButton];
    }
}

#pragma mark tabledelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    NSMutableDictionary * dic = [self.arrayTitle objectAtIndex:selectFatherIndex];
    
    NSMutableArray * array = [dic objectForKey:@"subCat"];
    return [array count];
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (selectSubIndex == section)
    {
        //第三节点title
        NSMutableDictionary * dic = [self.arrayTitle objectAtIndex:selectFatherIndex];//zititle
        NSMutableArray * arrr = [dic objectForKey:@"subCat"];
        NSMutableDictionary * dicSub = [arrr objectAtIndex:section];
        NSMutableArray * arraySub = [dicSub objectForKey:@"diseases"];
        return  arraySub.count;
    }else
        return 0;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

        NSString * CellIdentifier = @"LookingForDoctorCellStr";
        LookingForDoctorCell *cell=(LookingForDoctorCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if(!cell)
        {
            cell = (LookingForDoctorCell *)[[[NSBundle mainBundle] loadNibNamed:@"LookingForDoctorCell" owner:self options:nil] objectAtIndex:0];
        }
             //字典--数组--字典--数组--字典
          //  NSLog(@"==%ld",(long)self.selectFatherIndex);
            NSMutableDictionary * dic = [self.arrayTitle objectAtIndex:selectFatherIndex];
            NSMutableArray * arrr = [dic objectForKey:@"subCat"];
            NSMutableDictionary * dicSub = [arrr objectAtIndex:indexPath.section];
            NSMutableArray * arraySub = [dicSub objectForKey:@"diseases"];
            [cell fillTitleDataWith:[[arraySub objectAtIndex:indexPath.row] objectForKey:@"name"]];
        
        return cell;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
  
    return 45;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 45;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
        UIView* mView = [[UIView alloc]initWithFrame:CGRectMake(0, 0,tableTitle.width, 50)];
        [mView setBackgroundColor:UIColorFromRGB(239,239,237)];
        
        UIButton *bt = [UIButton buttonWithType:UIButtonTypeCustom];
        [bt setFrame:CGRectMake(0, 0,mView.width, mView.height)];
        [bt setTag:section];
        [bt addTarget:self action:@selector(didSelectCellRowidOpeno:) forControlEvents:UIControlEventTouchUpInside];
        [mView addSubview:bt];

        UIImageView *imageArrowIcon = [[UIImageView alloc]init];
        imageArrowIcon.frame = CGRectMake(10,(mView.height-22)/2, 22, 22);
 
        [imageArrowIcon setImage:[UIImage imageNamed:(selectSubIndex == section)?@"look_arrow_down.png":@"look_arrow_up.png"]];
    
        [mView addSubview:imageArrowIcon];
        
        UILabel * labtitle = [[UILabel alloc]init];
        labtitle.frame = CGRectMake(imageArrowIcon.right+15, 0, mView.width-imageArrowIcon.width-47, mView.height);
        [labtitle setFont:[UIFont systemFontOfSize:17]];
        NSMutableDictionary * dic = [self.arrayTitle objectAtIndex:selectFatherIndex];//zititle
        NSMutableArray * arrr = [dic objectForKey:@"subCat"];
        NSMutableDictionary * dicSub = [arrr objectAtIndex:section];
        NSString * strTitle = [dicSub objectForKey:@"name"];
        labtitle.text = strTitle;
        [mView addSubview:labtitle];
    
    return mView;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    LookingForDoctorCell * cell = (LookingForDoctorCell *)[tableView cellForRowAtIndexPath:indexPath];
    NSString * str = cell.labTitle.text;
    LookingDetailsViewController * details = [[LookingDetailsViewController alloc]initWithNibName:@"LookingDetailsViewController" bundle:nil];
    details.strTitle = str;
    
    //字典--数组--字典--数组--字典
    NSMutableDictionary * dic = [self.arrayTitle objectAtIndex:selectFatherIndex];
    NSMutableArray * arrr = [dic objectForKey:@"subCat"];
    NSMutableDictionary * dicSub = [arrr objectAtIndex:indexPath.section];
    NSMutableArray * arraySub = [dicSub objectForKey:@"diseases"];
   // NSLog(@"==%@",[arraySub objectAtIndex:indexPath.row]);
    details.strTypeID = [[arraySub objectAtIndex:indexPath.row] objectForKey:@"id"];
    details.strDiseaseHeadUrl = self.strDiseaseHeadUrl;
    details.strDoctorUrl = self.strDoctorUrl;
    //NSLog(@"==%@",self.strDiseaseHeadUrl);
    [self.navigationController pushViewController:details animated:YES];
}


- (void)didSelectCellRowidOpeno:(UIButton *)sender
{
    UIButton * btn = (UIButton *)sender;
    NSInteger index = btn.tag;
    selectSubIndex = (selectSubIndex == index)?-1:index;
    [tableTitle reloadData];
}





- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
